package net.oschina.app.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import net.oschina.app.R;
import net.oschina.app.bean.MyInformation;
import net.oschina.app.bean.User;
import net.oschina.app.constant.Constants;
import net.oschina.app.ui.BaseCommonTABActivity;
import net.oschina.app.util.MApp;
import net.oschina.app.util.URLGet;

import static net.oschina.app.R.id.ly_follower;
import static net.oschina.app.R.id.ly_following;
import static net.oschina.app.R.id.rl_message;
import static net.oschina.app.R.id.tv_follower;
import static net.oschina.app.R.id.tv_following;

/**
 * Created by YL on 2017/3/31.
 */

public class MyInformationFragment extends Fragment implements View.OnClickListener {

    private View mView;
    private LinearLayout mLl_favorite;
    private TextView mTv_favorite;
    private TextView mTv_following;
    private TextView mTv_follower;
    private LinearLayout mLl_follower;
    private LinearLayout mLl_following;
    private LinearLayout mLl_message;
    public static final int Zeor = 0;
    public static final int One = 1;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.myinformation_fragment, container, false);
        return mView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        initEvent();
    }

    //加载布局
    private void initView() {
        mLl_favorite = (LinearLayout) mView.findViewById(R.id.ly_favorite);
        mLl_following = (LinearLayout) mView.findViewById(ly_following);
        mLl_follower = (LinearLayout) mView.findViewById(ly_follower);
        mLl_message = (LinearLayout) mView.findViewById(rl_message);
        mTv_favorite = (TextView) mView.findViewById(R.id.tv_favorite);
        mTv_following = (TextView) mView.findViewById(tv_following);
        mTv_follower = (TextView) mView.findViewById(tv_follower);
    }

    //事件处理
    private void initEvent() {
        mLl_favorite.setOnClickListener(this);
        mLl_following.setOnClickListener(this);
        mLl_follower.setOnClickListener(this);
        mLl_message.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        String title = "";
        switch (view.getId()) {
            case R.id.ly_favorite:
                title = getResources().getString(R.string.actionbar_title_user_favorite);
                startTo(Zeor, "favorite", title);
                break;
            case R.id.ly_following:
                title = getResources().getString(R.string.actionbar_title_my_friends);
                startTo(Zeor, "fans", title);
                break;
            case R.id.ly_follower:
                title = getResources().getString(R.string.actionbar_title_my_friends);
                startTo(One, "fans", title);
                break;
            case R.id.rl_message:
                title = getResources().getString(R.string.actionbar_title_mes);
                startTo(Zeor, "msg", title);
                break;
        }
    }

    //打开收藏界面
    private void startTo(int index, String type, String title) {
        Intent intent = new Intent(getActivity(), BaseCommonTABActivity.class);
        intent.putExtra("index", index);
        intent.putExtra("type", type);
        intent.putExtra("title", title);
        startActivity(intent);
    }

    //在获得焦点的时候加载数据
    @Override
    public void onResume() {
        super.onResume();
        initData();
    }

    private void initData() {
        String path = Constants.Information_Path;
        URLGet urlGet = new URLGet();
        urlGet.getData(path, MyInformation.class, new URLGet.OnURLGetLitener<MyInformation>() {
            @Override
            public void showSuccess(MyInformation bean) {
                final User user = bean.getUser();

                MApp.mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        mTv_favorite.setText(user.getFavoritecount()+"");
                        mTv_following.setText(user.getFans()+"");
                        mTv_follower.setText(user.getFollowers()+"");
                    }
                });
            }

            @Override
            public void showError(Exception e) {

            }
        });
    }
}
